﻿using DotNet.Auth.Utility;
using DotNet.JCore.Entity;
using DotNet.JCore.Service;
using DotNet.Web.Areas.AuthWeb.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNet.Extensions;
using DotNet.Auth.Service;

namespace DotNet.Web.Areas.Mobile.Controllers
{
    public class M_SyncCarExitStationController : M_BaseController
    {
        private SyncCarExitStationService service = new SyncCarExitStationService();


        public ActionResult Search()
        {
            return View();
        }
        public ActionResult Index(string CarNO)
        {
            var currentUser = AuthHelper.GetSessionUser();
            if (!currentUser.IsAdmin)
            {
                ViewBag.pointId = CurrentEmployee.PointId;
            }
            ViewBag.buscode = CarNO;
            Session["con"] = null;
            return View();
        }

        private ActionResult NotFound(string id)
        {
            return NotFound("获取 同步车辆出站数据信息 错误", $"无法找到 主键 = {id} 的同步车辆出站数据信息");
        }


        /// <summary>
        /// 获取当天未稽查数据列表
        /// </summary>
        /// <param name="CarNO">车牌号</param>
        /// <param name="Point">稽查点</param>
        /// <param name="Station">客运站</param>
        /// <param name="Time">时间段：单位（分钟）</param>
        /// <returns></returns>
        public ActionResult Grid(string CarNO)//,string startDate
        {
            Session["con"] = CarNO;
            var IsSearchNoPointCar = AuthService.Param.Get("IsSearchNoPointCar", "1");
            var list = service.GetList(CarNO, IsSearchNoPointCar);//, startDate.ToDateTimeOrNull()
            return View(list);
        }

        public ActionResult Edit(string id)
        {
            var entity = service.Get(id);
            entity.CheckStatus = 1;
            entity.ProcessStatus = false;
            entity.Note = "系统违规登记";
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(SyncCarExitStationModel entity)
        {
            return View("Edit", entity);
        }

        public ActionResult Details(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return View(entity);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Note"></param>
        /// <returns></returns>
        public ActionResult Confrim(string ID, string Note)
        {
            SyncCarExitStationModel entity = service.Get(ID);
            entity.PoinID = CurrentEmployee.PointId;
            entity.PointName = CurrentEmployee.PointName;
            entity.Note = Note+ "（移动端登记）";
            entity.CheckStatus = 0;
            AuthHelper.SetJCUserInfo(entity, true);
            var result = service.SaveConfrim(entity );
            return Json(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="OffenceId"></param>
        /// <param name="Offence"></param>
        /// <param name="Note"></param>
        /// <param name=""></param>
        /// <param name=""></param>
        /// <returns></returns>
        public ActionResult Infraction(string ID, string OffenceId, string Offence, string Note)
        {
            SyncCarExitStationModel entity = service.Get(ID);
            entity.PoinID = CurrentEmployee.PointId;
            entity.PointName = CurrentEmployee.PointName;
            entity.OffenceId = OffenceId;
            entity.OffenceName = Offence;
            entity.Note = Note+"（移动端登记）";
            entity.CheckStatus = 1;
            AuthHelper.SetJCUserInfo(entity, true);
            var result = service.SaveInfraction(entity);
            return Json(result);
        }


        public ActionResult Save(SyncCarExitStationModel entity)
        {
            string BusCode = (string)Session["con"];
            entity.PoinID = CurrentEmployee.PointId;
            entity.PointName = CurrentEmployee.PointName;
            AuthHelper.SetJCUserInfo(entity, true);
            var action = Url.Action("Index", "M_SyncCarExitStation", new { CarNO = BusCode });
            var result = service.Save(entity, true);
            if (result.Success)
            {
                return Json(new { success = true, message = "保存成功", url = action });
            }
            else
            {
                return Json(new { success = false, message = "保存失败", url = action });
            }
            //return Json(result);
            //return RedirectToAction("Index", "M_SyncCarExitStation", new { CarNO= BusCode });//,new { CarNO=ViewBag.buscode }
        }

        //public ActionResult Confirm(string id)
        //{
        //    var entity = service.Get(id);

        //    entity.CheckStatus = 0;
        //    entity.ProcessStatus = true;
        //    entity.Note = "系统正常登记";
        //    AuthHelper.SetJCUserInfo(entity, true);
        //    if (entity == null) return NotFound(id);
        //    return View("Confirm", entity);
        //}


        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Confirm(SyncCarExitStationModel entity)
        //{
        //    entity.PoinID = CurrentEmployee.PointId;
        //    entity.PointName = CurrentEmployee.PointName;
        //    AuthHelper.SetJCUserInfo(entity, true);
        //    var result = service.Save(entity, true);
        //    //return Json(result);
        //    return RedirectToAction("Index", "M_SyncCarExitStation");//,new { CarNO=ViewBag.buscode }
        //}
    }
}